package com.scoring.system.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.scoring.system.mapper.JurisdictionMapper;
import com.scoring.system.mapper.UserJurisdictionMapper;
import com.scoring.system.mapper.UserMapper;
import com.scoring.system.pojo.Jurisdiction;
import com.scoring.system.pojo.User;
import com.scoring.system.pojo.UserJurisdiction;
import com.scoring.system.service.UserService;
@Service("userService")
public class UserServiceImpl implements UserService {
	@Resource
	private UserMapper userDao;
	
	@Resource
	private UserJurisdictionMapper userJdDao;
	
	@Resource
	private JurisdictionMapper jdDao;

	@Override
	public int softDeleteUser(int userId) {
		return userDao.softDelete(userId);
	}

	@Override
	public int saveUser(User record) {
		return userDao.insert(record);
	}

	@Override
	public boolean canUserLogin(String userNumber, String password, String jurisdictionName) {
		User user=userDao.selectByUserNumberAndPassword(userNumber, password);
		if(user!=null){
			Jurisdiction jurisdiction =jdDao.selectByName(jurisdictionName);
			UserJurisdiction userJurisdiction = userJdDao.selectByUserId(user.getId());
			if(userJurisdiction.getJurisdictionId()==jurisdiction.getId()){
				return true;
			}else{
				return false;
			}
		}
		return false;
	}

	@Override
	public boolean isUserExisted(String userNumber) {
		return userDao.selectByUserNumber(userNumber)!=null;
	}

	@Override
	public User getUserById(int userId) {
		return userDao.selectByPrimaryKey(userId);
	}

	@Override
	public User getUserByUserNumber(String userNumber) {
		return userDao.selectByUserNumber(userNumber);
	}

	@Override
	public User getUserByName(String name) {
		return userDao.selectByName(name);
	}

	@Override
	public List<User> listUser() {
		return userDao.listUser();
	}

	@Override
	public int updateUser(User record) {
		return userDao.updateByPrimaryKeySelective(record);
	}

	@Override
	public int saveUserJurisdiction(UserJurisdiction record) {
		return userJdDao.insert(record);
	}

	@Override
	public UserJurisdiction getUserJurisdictionById(int id) {
		return userJdDao.selectByPrimaryKey(id);
	}


	@Override
	public int updateUserJurisdiction(UserJurisdiction record) {
		return userJdDao.updateByPrimaryKeySelective(record);
	}

	@Override
	public UserJurisdiction getUserJurisdictionByUserId(int userId) {
		return userJdDao.selectByUserId(userId);
	}

}
